package org.lmd.platform.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.lmd.platform.annotation.PlLog;
import org.lmd.platform.controller.BaseController;
import org.lmd.platform.entity.sys.ReturnObjct;
import org.lmd.platform.entity.sys.RoleEntity;
import org.lmd.platform.sys.RoleService;
import org.lmd.platform.sys.UserService;
import org.lmd.platform.validator.ValidatorUtils;
import org.lmd.platform.vo.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

   
    /**
     * 角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:role:select")
    public ReturnObjct select(){
        List<RoleEntity> list = roleService.selectList(null);

        return ReturnObjct.ok().put("list", list);
    }
    /**
     * 保存角色
     */
    @PlLog("保存角色")
    @RequestMapping("/save")
    @RequiresPermissions("sys:role:save")
    public ReturnObjct save(@RequestBody RoleEntity role){
        ValidatorUtils.validateEntity(role);

        roleService.save(role);

        return ReturnObjct.ok();
    }
    /**
     * 修改角色
     */
    @PlLog("修改角色")
    @RequestMapping("/update")
    @RequiresPermissions("sys:role:update")
    public ReturnObjct update(@RequestBody RoleEntity role){
        ValidatorUtils.validateEntity(role);

        this.roleService.update(role);

        return ReturnObjct.ok();
    }
    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
    public ReturnObjct info(@PathVariable("roleId") Long roleId){
        RoleEntity role = roleService.selectById(roleId);

        //查询角色对应的菜单
        List<Long> menuIdList = roleService.queryRoleMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        //查询角色对应的部门
        List<Long> deptIdList = roleService.queryRoleDeptIdList(new Long[]{roleId});
        role.setDeptIdList(deptIdList);

        return ReturnObjct.ok().put("role", role);
    }
    /**
     * 角色列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:role:list")
    public ReturnObjct list(@RequestParam Map<String, Object> params){
        PageUtils page = roleService.queryPage(params);

        return ReturnObjct.ok().put("page", page);
    }
    /**
     * 删除角色
     */
    @PlLog("删除角色")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public ReturnObjct delete(@RequestBody Long[] roleIds){
        roleService.deleteBatch(roleIds);
        return ReturnObjct.ok();
    }
}
